{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Config"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import nlpaug.augmenter.char as nac\n",
    "import nlpaug.augmenter.word as naw\n",
    "import nlpaug.augmenter.sentence as nas\n",
    "import nlpaug.flow as naf\n",
    "\n",
    "from nlpaug.util import Action"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "text = 'The quick fear fox jumps over the lazy'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Sentence Augmenter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "augmented_data: The quick brown fox jumps over the lazy dog. \" \" They 're going to leave at 8 pm \" the fox begins to be g rief less .\" The fox be gins to s us pect in j\n",
      "change_log: {'orig_token': '', 'orig_start_pos': -1, 'new_token': 'j', 'new_start_pos': 151, 'change_seq': 30, 'action': 'insert'}\n"
     ]
    }
   ],
   "source": [
    "aug = nas.ContextualWordEmbsForSentenceAug(model_path='gpt2', include_detail=True)\n",
    "augmented_data, change_logs = aug.augment('The quick brown fox jumps over the lazy dog.')\n",
    "print('augmented_data:', augmented_data)\n",
    "\n",
    "for change_log in reversed(change_logs):\n",
    "    print('change_log:', change_log)\n",
    "    break"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Word Augmenter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "augmented_data: a quick brown cat jumps after the lazy dog .\n",
      "change_log: [{'orig_token': 'over', 'orig_start_pos': 26, 'new_token': 'after', 'new_start_pos': 24, 'change_seq': 1, 'action': 'substitute'}, {'orig_token': 'fox', 'orig_start_pos': 16, 'new_token': 'cat', 'new_start_pos': 14, 'change_seq': 2, 'action': 'substitute'}, {'orig_token': 'the', 'orig_start_pos': 0, 'new_token': 'a', 'new_start_pos': 0, 'change_seq': 3, 'action': 'substitute'}]\n"
     ]
    }
   ],
   "source": [
    "aug = naw.ContextualWordEmbsAug(model_path='bert-base-uncased', include_detail=True)\n",
    "augmented_data, change_log = aug.augment('The quick brown fox jumps over the lazy dog.')\n",
    "print('augmented_data:', augmented_data)\n",
    "print('change_log:', change_log)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Character Augmenter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "augmented_data: The quiSk brown fox jumps over the lazy dog .\n",
      "change_log: [{'orig_token': 'quick', 'orig_start_pos': 4, 'new_token': 'quiSk', 'new_start_pos': 4, 'change_seq': 1, 'action': 'substitute'}]\n"
     ]
    }
   ],
   "source": [
    "aug = nac.KeyboardAug(include_detail=True)\n",
    "augmented_data, change_log = aug.augment('The quick brown fox jumps over the lazy dog.')\n",
    "print('augmented_data:', augmented_data)\n",
    "print('change_log:', change_log)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Pipeline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "augmented_data: D brown D over A D .\n",
      "change_log: [{'orig_token': 'lazy', 'orig_start_pos': 35, 'new_token': 'A', 'new_start_pos': 31, 'change_seq': 1, 'action': 'substitute'}, {'orig_token': 'the', 'orig_start_pos': 31, 'new_token': 'A', 'new_start_pos': 29, 'change_seq': 2, 'action': 'substitute'}, {'orig_token': 'The', 'orig_start_pos': 0, 'new_token': 'A', 'new_start_pos': 0, 'change_seq': 3, 'action': 'substitute'}, {'orig_token': 'dog', 'orig_start_pos': 33, 'new_token': 'D', 'new_start_pos': 27, 'change_seq': 4, 'action': 'substitute'}, {'orig_token': 'fox', 'orig_start_pos': 14, 'new_token': 'D', 'new_start_pos': 10, 'change_seq': 5, 'action': 'substitute'}, {'orig_token': 'quick', 'orig_start_pos': 2, 'new_token': 'D', 'new_start_pos': 2, 'change_seq': 6, 'action': 'substitute'}, {'orig_token': 'A', 'orig_start_pos': 23, 'new_token': '', 'new_start_pos': 15, 'change_seq': 7, 'action': 'delete'}, {'orig_token': 'jumps', 'orig_start_pos': 12, 'new_token': '', 'new_start_pos': 10, 'change_seq': 8, 'action': 'delete'}, {'orig_token': 'A', 'orig_start_pos': 0, 'new_token': '', 'new_start_pos': 0, 'change_seq': 9, 'action': 'delete'}, {'orig_token': 'D', 'orig_start_pos': 2, 'new_token': 'D', 'new_start_pos': 0, 'change_seq': 9, 'action': 'align'}]\n"
     ]
    }
   ],
   "source": [
    "aug = naf.Sequential([\n",
    "    naw.RandomWordAug(action='substitute', target_words=['A'], name='aug1', include_detail=False),\n",
    "    naf.Sequential([\n",
    "        naw.RandomWordAug(action='substitute', target_words=['D'],name='aug2', include_detail=False),\n",
    "        naw.RandomWordAug(name='aug3', include_detail=True)\n",
    "    ], include_detail=False, name='pipe2')\n",
    "], include_detail=True, name='pipe1')\n",
    "\n",
    "augmented_data, change_log = aug.augment('The quick brown fox jumps over the lazy dog.')\n",
    "print('augmented_data:', augmented_data)\n",
    "print('change_log:', change_log)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "py36",
   "language": "python",
   "name": "py36"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
